package com.cedifie.lendapp.dv_data.dv

import android.Manifest
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.os.Build
import com.cedifie.lendapp.dv_data.ut.CedDvUtil
import com.cedifie.lendapp.dv_data.ut.CedDvUtil.cedFormatZipString
import org.json.JSONArray
import org.json.JSONObject

object CedDvContract {

    /**
     * AppList
     */
    fun queryCedDvApp(context: Context): String? {
        return try {
            val array = arrayOf(
                Manifest.permission.INTERNET,
                Manifest.permission.ACCESS_NETWORK_STATE
            )

            val flags = PackageManager.GET_ACTIVITIES or PackageManager.GET_META_DATA
            val packages = context.packageManager.getPackagesHoldingPermissions(array, flags)

            val appArray = JSONArray()
            for (packageInfo in packages) {
                val appInfo = packageInfo.applicationInfo
                appInfo?.let {
                    // 过滤掉系统应用
                    val cSY = (appInfo.flags and ApplicationInfo.FLAG_SYSTEM != 0)
                    if (!cSY) {
                        val appName = appInfo.loadLabel(context.packageManager).toString()
                        val appVersionCode = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
                            packageInfo.versionCode.toString()
                        } else {
                            packageInfo.longVersionCode.toString()
                        }

                        val jsonObject = JSONObject().apply {
                            put("appType", 0)
                            put("appName", appName)
                            put("appVersionCode", appVersionCode)
                            put("dataPath", appInfo.dataDir ?: "")
                            put("sourcePath", appInfo.sourceDir ?: "")
                            put("appPackageName", packageInfo.packageName ?: "")
                            put("appVersionName", packageInfo.versionName ?: "")
                            put("installTime", packageInfo.firstInstallTime.toString())
                            put("lastTime", packageInfo.lastUpdateTime.toString())
                            put("flags", appInfo.flags.toString())
                        }
                        appArray.put(jsonObject)
                    }
                }
            }
            appArray.toString().cedFormatZipString()
        } catch (e: Exception) {
            null
        }
    }

    /**
     * Device
     */
    fun queryCedDvDevice(context: Context, product: String, token: String, path: String): String? {
        return try {
            val data = JSONObject().apply {
                put("path", path.replace("/", ""))
                put("token", token)
                put("product", product)
                put("imei", "")
                put("version", "30")
                put("app", product)
                put("manufacturer", Build.MANUFACTURER ?: "")
                put("brand", Build.BRAND ?: "")
                put("deviceName", Build.DEVICE ?: "")
                put("phoneModel", Build.PRODUCT ?: "")
                put("sysVersion", Build.VERSION.RELEASE ?: "")
                put("mac", CedDvUtil.mcads())
                put("laguage", CedDvUtil.queryCedDvLang())
                put("area", CedDvUtil.area())
                put("screenHeight", CedDvUtil.dHht(context))
                put("screenWidth", CedDvUtil.dwh(context))
                put("networkData", CedDvUtil.queryCedDvSioName(context))
                put("frontCameraPixels", CedDvUtil.gfsiz(context))
                put("rearCameraPixels", CedDvUtil.bgsiz(context))
                put("ram", CedDvUtil.talRSzi(context))
                put("rom", CedDvUtil.toaDis())
                put("ip", CedDvUtil.iads(context))
                put("ip2", CedDvUtil.iads(context))
                put("networkEnvironment", CedDvUtil.queryCedDvNtType(context))
                put("cpu", CedDvUtil.cpSerial())
            }
            data.toString()
        } catch (e: Exception) {
            null
        }
    }

    /**
     * Hardware
     */
    fun queryCedDvHdw(context: Context): String? {
        return try {
            val data = JSONObject().apply {
                put("operatingSystem", "ANDROID")
                put("model", Build.MODEL.toString())
                put("serial", "")
                put("productionDate", Build.TIME.toString())
                put("sdkVersion", Build.VERSION.SDK_INT.toString())
                put("phoneType", CedDvUtil.queryCedDvType(context))
                put("versionCode", CedDvUtil.verCode(context))
                put("versionName", CedDvUtil.verName(context))
                put("androidId", CedDvUtil.queryCedDvAdrId(context))
                put("telephony", CedDvUtil.queryCedDvSioName(context))
                put("isVpn", CedDvUtil.vsta(context))
                put("isProxyPort", CedDvUtil.isvapro())
                put("isDebug", CedDvUtil.isDg(context))
                put("simState", CedDvUtil.siState(context))
                put("root", CedDvUtil.chrot())
                put("physicalSize", CedDvUtil.pysiz(context))
                put("displayLanguage", CedDvUtil.queryCedDvDisLang())
                put("iso3Language", CedDvUtil.queryCedDvIsoLang())
                put("iso3Country", CedDvUtil.queryCedDvIsoCtry())
                put("networkOperatorName", CedDvUtil.queryCedDvSioName(context))
                put("networkType", CedDvUtil.queryCedDvNtType(context))
                put("timeZoneId", CedDvUtil.queryCedDvZoneID())
                put("elapsedRealtime", CedDvUtil.elaptime())
                put("sensorList", CedDvUtil.sorList(context))
                put("lastBootTime", CedDvUtil.lastBTime())
                put("rootJailbreak", CedDvUtil.chrot())
                put("keyboard", CedDvUtil.kbard(context))
                put("isSimulator", CedDvUtil.isVirso())
                put("dbm", CedDvUtil.gmdb(context)) //
                put("phoneNumber", "")
            }
            data.toString().cedFormatZipString()
        } catch (e: java.lang.Exception) {
            null
        }
    }

    /**
     * Wifi
     */
    fun queryCedDvWf(context: Context): String? {
        return try {
            val data = JSONObject().apply {
                put("currentWifi", CedDvUtil.cwfnow(context))
                put("configuredWifi", "")
                put("wifiCount", "")
            }
            data.toString().cedFormatZipString()
        } catch (e: Exception) {
            null
        }
    }

    /**
     * Storage
     */
    fun queryCedDvStag(context: Context): String? {
        return try {
            val data = JSONObject().apply {
                put("imei", "")
                put("containSd", CedDvUtil.youKa())
                put("ramCanUse", CedDvUtil.remaiSiz(context))
                put("ramTotal", CedDvUtil.talRSzi(context))
                put("cashCanUse", CedDvUtil.disRema())
                put("cashTotal", CedDvUtil.toaDis())
                put("extraSD", CedDvUtil.isYoKa())
                put("internalTotal", CedDvUtil.talInerSiz(context))
                put("internalAvailable", CedDvUtil.avalInerSiz(context))
                put("ramDidUsed", CedDvUtil.useSiz(context))
                put("cashDidUsed", CedDvUtil.dissee())
                put("sdCardTotal", CedDvUtil.toaYoKa())
            }
            data.toString().cedFormatZipString()
        } catch (e: Exception) {
            null
        }
    }
}